package com.ywjt.platformbusiness.store.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.platformbusiness.EventBusBean;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseVPFragment;
import com.ywjt.platformbusiness.common.MethodUtils;
import com.ywjt.platformbusiness.common.UserData;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.login.LoginActivity;
import com.ywjt.platformbusiness.login.model.LoginModel;
import com.ywjt.platformbusiness.main.MainActivity;
import com.ywjt.platformbusiness.message.BusinessMessageActivity;
import com.ywjt.platformbusiness.my.activity.ActivityWithdraw;
import com.ywjt.platformbusiness.store.activity.ActivityBusinessInfo;
import com.ywjt.platformbusiness.store.activity.ActivityBusinessPostGoods;
import com.ywjt.platformbusiness.store.activity.BusinessGoodsTrack;
import com.ywjt.platformbusiness.store.activity.BusinessManagementActivity;
import com.ywjt.platformbusiness.store.model.BusinessCenterMsgModle;
import com.ywjt.platformbusiness.store.model.BusinessHomeInfoModel;
import com.ywjt.platformbusiness.store.order.activity.StoreOrderList;
import com.ywjt.platformbusiness.store.adapter.BusinessMailAdapter;
import com.ywjt.platformbusiness.util.JsonUtils;
import com.ywjt.platformbusiness.util.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BusinessCenterFragment extends BaseVPFragment implements View.OnClickListener {
    private ImageView ivMsg,ivChange;
    private RecyclerView rcCommon;
    private BusinessMailAdapter adapter;
    private List<String> data;
    private LinearLayout llPostGoods;
    private View view;
    private Context context;
    private LinearLayout llOrderMamager,llBusinessManagement,llGoodsManament;
    private TextView tvSubmit,tvStoreName,tvBalance,tvTodayMoney,tvTotalMoney,tvNewAddOrder;
    private ImageView ivStoreLogo;

    public static BusinessCenterFragment newInstance( ) {
        Bundle arguments = new Bundle();
        BusinessCenterFragment fl = new BusinessCenterFragment();
        fl.setArguments(arguments);
        return fl;
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.business_center_fragment, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivMsg:
                BusinessMessageActivity.actionStart(context);
                break;
            case R.id.llPostGoods:
                //我要寄件
                ActivityBusinessPostGoods.actionStart(context);
                break;
            case R.id.llOrderMamager:
                StoreOrderList.actionStart(context);
                break;
            case R.id.llBusinessManagement:
                BusinessManagementActivity.actionStart(context);
                break;
            case R.id.llGoodsManament:
                EventBusBean busBean=new EventBusBean();
                busBean.setCode(100);
                EventBus.getDefault().post(busBean);
                break;
            case R.id.ivChange:
                MainActivity.actionStart(context,3);
                break;
            case R.id.tvSubmit:
                //提现
                ActivityWithdraw.actionStart(context);
                break;
            case R.id.ivStoreLogo:
                ActivityBusinessInfo.actionStart(context);
                break;
        }
    }

    public void initView() {
        llOrderMamager=view.findViewById(R.id.llOrderMamager);
        llGoodsManament=view.findViewById(R.id.llGoodsManament);
        llBusinessManagement=view.findViewById(R.id.llBusinessManagement);
        ivMsg=view.findViewById(R.id.ivMsg);
        ivChange=view.findViewById(R.id.ivChange);
        tvNewAddOrder=view.findViewById(R.id.tvNewAddOrder);
        tvBalance=view.findViewById(R.id.tvBalance);
        tvSubmit=view.findViewById(R.id.tvSubmit);
        ivStoreLogo=view.findViewById(R.id.ivStoreLogo);
        tvStoreName=view.findViewById(R.id.tvStoreName);
        tvTodayMoney=view.findViewById(R.id.tvTodayMoney);
        tvTotalMoney=view.findViewById(R.id.tvTotalMoney);

        rcCommon=view.findViewById(R.id.rcCommon);
        llPostGoods=view.findViewById(R.id.llPostGoods);
        data=new ArrayList<>();
        data.add("");
        data.add("");
        data.add("");
        adapter=new BusinessMailAdapter(data);
        rcCommon.setAdapter(adapter);
        rcCommon.setLayoutManager(new LinearLayoutManager(context));
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BusinessGoodsTrack.actionStart(context);
            }
        });

        llPostGoods.setOnClickListener(this::onClick);
        llOrderMamager.setOnClickListener(this::onClick);
        tvSubmit.setOnClickListener(this::onClick);
        ivStoreLogo.setOnClickListener(this::onClick);
        ivChange.setOnClickListener(this::onClick);
        ivMsg.setOnClickListener(this::onClick);
        llBusinessManagement.setOnClickListener(this::onClick);
        llGoodsManament.setOnClickListener(this::onClick);
    }


    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    /**
     * 在fragment首次可见时回调，可用于加载数据，防止每次进入都重复加载数据
     */
    @Override
    protected void onFragmentFirstVisible() {
        getCenterData();
    }

    private void getCenterData() {
        new HttpRequest(getContext()).doPost(UrlConstants.merchant_information+UserData.create(context).get(UserData.MERCHANTID), "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), BusinessHomeInfoModel.class);
                        if (obj instanceof BusinessHomeInfoModel) {
                            BusinessHomeInfoModel model = (BusinessHomeInfoModel) obj;
                            Glide.with(context).load(model.getResult().getShopLogo())
                                    .placeholder(R.mipmap.icon_business_logo)
                                    .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(10)))
                                    .into(ivStoreLogo);

                            tvStoreName.setText(model.getResult().getShopName());
                            tvBalance.setText(model.getResult().getBalance()+"");
                            tvTodayMoney.setText(model.getResult().getTodayDealRental()+"");
                            tvTotalMoney.setText(model.getResult().getDealRental()+"");
                            tvNewAddOrder.setText(model.getResult().getNewOrderNum()+"");
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }
}
